package com.lottery.service.pc28.statisticsService.impl;

import com.lottery.dao.pc28.statisticsDao.ConstantTatisticsDao;
import com.lottery.dto.ResultDto;
import com.lottery.entity.ConstantTatistics;
import com.lottery.service.pc28.JedisClient;
import com.lottery.service.pc28.statisticsService.ConstantTatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ConstantTatisticsServiceImpl implements ConstantTatisticsService {

	@Resource
	private ConstantTatisticsDao constantTatisticsDao;

	@Autowired
	private JedisClient jedisClient;

	/**
	 * 初始化统计
	 */
	@Override
	public ResultDto<List<Map<String, String>>> selectConstantTatistics() {
		ResultDto<List<Map<String, String>>> resultDto = new ResultDto<List<Map<String, String>>>();
		try {
			ConstantTatistics constantTatistics = new ConstantTatistics();
			String nextExpect = jedisClient.get("nextExpect");
			constantTatistics.setExpect(nextExpect);
			// 统计玩法
			List<ConstantTatistics> list = constantTatisticsDao.statisticalMechanics(constantTatistics);
			long timeMillis = System.currentTimeMillis();
			if (list.isEmpty()) {
				ConstantTatistics tatistics1 = new ConstantTatistics();
				tatistics1.setExpect(nextExpect);
				tatistics1.setMoney(0);
				tatistics1.setPlayingMethod("大小单双玩法");
				tatistics1.setDate(timeMillis);// 设置当前时间搓
				list.add(tatistics1);

				ConstantTatistics tatistics2 = new ConstantTatistics();
				tatistics2.setExpect(nextExpect);
				tatistics2.setMoney(0);
				tatistics2.setPlayingMethod("数字玩法");
				tatistics2.setDate(timeMillis);
				list.add(tatistics2);

				ConstantTatistics tatistics3 = new ConstantTatistics();
				tatistics3.setExpect(nextExpect);
				tatistics3.setMoney(0);
				tatistics3.setPlayingMethod("猜合");
				tatistics3.setDate(timeMillis);
				list.add(tatistics3);

				ConstantTatistics tatistics4 = new ConstantTatistics();
				tatistics4.setExpect(nextExpect);
				tatistics4.setMoney(0);
				tatistics4.setPlayingMethod("数字组合玩法");
				tatistics4.setDate(timeMillis);
				list.add(tatistics4);

				ConstantTatistics tatistics5 = new ConstantTatistics();
				tatistics5.setExpect(nextExpect);
				tatistics5.setMoney(0);
				tatistics5.setPlayingMethod("数字定位玩法");
				tatistics5.setDate(timeMillis);
				list.add(tatistics5);
			} else {
				List<String> playingMethodList = new ArrayList<String>();
				for (ConstantTatistics constantTatistics2 : list) {
					String playingMethod = constantTatistics2.getPlayingMethod();
					if (playingMethod.equals("大小单双玩法")) {
						constantTatistics2.setDate(timeMillis);
					}
					if (playingMethod.equals("数字玩法")) {
						constantTatistics2.setDate(timeMillis);
					}
					if (playingMethod.equals("猜合")) {
						constantTatistics2.setDate(timeMillis);
					}
					if (playingMethod.equals("数字组合玩法")) {
						constantTatistics2.setDate(timeMillis);
					}
					if (playingMethod.equals("数字定位玩法")) {
						constantTatistics2.setDate(timeMillis);
					}
					playingMethodList.add(playingMethod);
				}
				boolean contains1 = playingMethodList.contains("大小单双玩法");
				if (!contains1) {
					ConstantTatistics tatistics = new ConstantTatistics();
					tatistics.setExpect(nextExpect);
					tatistics.setMoney(0);
					tatistics.setPlayingMethod("大小单双玩法");
					tatistics.setDate(timeMillis);
					list.add(tatistics);
				}
				boolean contains2 = playingMethodList.contains("数字玩法");
				if (!contains2) {
					ConstantTatistics tatistics = new ConstantTatistics();
					tatistics.setExpect(nextExpect);
					tatistics.setMoney(0);
					tatistics.setPlayingMethod("数字玩法");
					tatistics.setDate(timeMillis);
					list.add(tatistics);
				}
				boolean contains3 = playingMethodList.contains("猜合");
				if (!contains3) {
					ConstantTatistics tatistics = new ConstantTatistics();
					tatistics.setExpect(nextExpect);
					tatistics.setMoney(0);
					tatistics.setPlayingMethod("猜合");
					tatistics.setDate(timeMillis);
					list.add(tatistics);
				}
				boolean contains4 = playingMethodList.contains("数字组合玩法");
				if (!contains4) {
					ConstantTatistics tatistics = new ConstantTatistics();
					tatistics.setExpect(nextExpect);
					tatistics.setMoney(0);
					tatistics.setPlayingMethod("数字组合玩法");
					tatistics.setDate(timeMillis);
					list.add(tatistics);
				}
				boolean contains5 = playingMethodList.contains("数字定位玩法");
				if (!contains5) {
					ConstantTatistics tatistics = new ConstantTatistics();
					tatistics.setExpect(nextExpect);
					tatistics.setMoney(0);
					tatistics.setPlayingMethod("数字定位玩法");
					tatistics.setDate(timeMillis);
					list.add(tatistics);
				}
			}
			// 统计总额
			ConstantTatistics statisticalMoney = constantTatisticsDao.statisticalMoney(constantTatistics);
			statisticalMoney.setDate(timeMillis);
			list.add(statisticalMoney);
			List<Map<String, String>> listDto = new ArrayList<Map<String, String>>();
			for (ConstantTatistics constantTatistics2 : list) {
				if (constantTatistics2.getPlayingMethod().equals("数字定位玩法")) {
					Map<String, String> map = new HashMap<String, String>();
					map.put("pm", "数字定位玩法");
					map.put("money", String.valueOf(constantTatistics2.getMoney()));
					map.put("date", String.valueOf(constantTatistics2.getDate()));
					listDto.add(map);
				}
				if (constantTatistics2.getPlayingMethod().equals("数字玩法")) {
					Map<String, String> map = new HashMap<String, String>();
					map.put("pm", "数字玩法");
					map.put("money", String.valueOf(constantTatistics2.getMoney()));
					map.put("date", String.valueOf(constantTatistics2.getDate()));
					listDto.add(map);
				}
				if (constantTatistics2.getPlayingMethod().equals("数字组合玩法")) {
					Map<String, String> map = new HashMap<String, String>();
					map.put("pm", "数字组合玩法");
					map.put("money", String.valueOf(constantTatistics2.getMoney()));
					map.put("date", String.valueOf(constantTatistics2.getDate()));
					listDto.add(map);
				}
				if (constantTatistics2.getPlayingMethod().equals("猜合")) {
					Map<String, String> map = new HashMap<String, String>();
					map.put("pm", "猜合");
					map.put("money", String.valueOf(constantTatistics2.getMoney()));
					map.put("date", String.valueOf(constantTatistics2.getDate()));
					listDto.add(map);
				}
				if (constantTatistics2.getPlayingMethod().equals("大小单双玩法")) {
					Map<String, String> map = new HashMap<String, String>();
					map.put("pm", "大小单双玩法");
					map.put("money", String.valueOf(constantTatistics2.getMoney()));
					map.put("date", String.valueOf(constantTatistics2.getDate()));
					listDto.add(map);
				}
				if (constantTatistics2.getPlayingMethod().equals("总额")) {
					Map<String, String> map = new HashMap<String, String>();
					map.put("pm", "总额");
					map.put("money", String.valueOf(constantTatistics2.getMoney()));
					map.put("date", String.valueOf(constantTatistics2.getDate()));
					listDto.add(map);
				}
			}
			resultDto.setList(listDto);
			resultDto.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultDto.setSuccess(false);
		}
		return resultDto;
	}

	/**
	 * 时时统计
	 */
	@Override
	public Object[][] selectConstantTatisticss() {
		Object[][] a = new Object[6][2];
		ResultDto<List<Map<String, String>>> resultDto = selectConstantTatistics();
		List<Map<String, String>> list = (List<Map<String, String>>) resultDto.getList();
		for (Map<String, String> map : list) {
			if (map.get("pm").equals("数字定位玩法")) {
				Long x = Long.valueOf(map.get("date"));
				double y = Double.valueOf(map.get("money"));
				a[0][0] = x;
				a[0][1] = y;
			}
			if (map.get("pm").equals("大小单双玩法")) {
				Long x = Long.valueOf(map.get("date"));
				double y = Double.valueOf(map.get("money"));
				a[1][0] = x;
				a[1][1] = y;
			}
			if (map.get("pm").equals("数字组合玩法")) {
				Long x = Long.valueOf(map.get("date"));
				double y = Double.valueOf(map.get("money"));
				a[2][0] = x;
				a[2][1] = y;
			}
			if (map.get("pm").equals("数字玩法")) {
				Long x = Long.valueOf(map.get("date"));
				double y = Double.valueOf(map.get("money"));
				a[3][0] = x;
				a[3][1] = y;
			}
			if (map.get("pm").equals("猜合")) {
				Long x = Long.valueOf(map.get("date"));
				double y = Double.valueOf(map.get("money"));
				a[4][0] = x;
				a[4][1] = y;
			}
			if (map.get("pm").equals("总额")) {
				Long x = Long.valueOf(map.get("date"));
				double y = Double.valueOf(map.get("money"));
				a[5][0] = x;
				a[5][1] = y;
			}
		}
		return a;
	}

}
